<template>
	<view class="success-wrapper" v-if="finish">
		<view class="pay-line">
			<image src="../../static/ic_wxpay.png"></image>
			<text>支付成功 </text>
		</view>
		<view class="info-line">
			<view>上海医益护健康 </view>
			<view class='price-line'>
				<span>￥</span>
				<span class="price">{{price}}</span>
			</view>
		</view>
		<view class="bottom" @click="finishPay">
			<view class="btn">完成</view>
		</view>
	</view>
</template>
<script>
	import util from "../../util/textUtil.js"
	export default {
		data() {
			return {
				price: "",
				finish: false,
				tradeId: ""
			}
		},
		onLoad(options) {
			uni.setNavigationBarTitle({
				title: "支付"
			});
			this.price = options.price;
			this.tradeId = options.tradeId;
			//this.repay(options.tradeId);
			this.getUserInfo();
		},
		methods: {
			finishPay: function() {
				uni.navigateBack({
					delta: 1
				});
			},
			getUserInfo: function() {
				uni.request({
					url:getApp().globalData.baseUrl +  '/sys/user/info',
					method: 'GET',
					header: {
						"token": getApp().globalData.token
					},
					success: res => {
						if (res.data.code == 0) {
                         this.repay(res.data.data.userId);
						}
					},
					fail: () => {},
					complete: () => {}
				});
			},
			repay: function(userId) {
				uni.request({
					url: getApp().globalData.baseUrl + 'order/tranrecord/allinPay',
					method: 'POST',
					data: {
						"openId": userId,
						"source": "wx",
						"tradeId": this.tradeId, // tradeId
						"type": 4 // 支付标识 3 微信  4支付宝
					},
					header: {
						"token": getApp().globalData.token
					},
					success: res => {
						console.log(res);
						if (res.data.code == 0) {
							this.$wxPay(res.data.data, function(res) {
								this.finish = true;
								/*成功的回调*/
								console.log("支付成功")
								// uni.navigateTo({
								// 	url: "success?price=" + this.price
								// });
							}, function(e) {
								this.finish = false;
								uni.showToast({
									title: e,
									icon: 'none'
								});
								/**失败的回调*/
								console.log("支付失败或取消支付")
							})
						} else {
							uni.showToast({
								title: res.data.msg,
								icon: 'none'
							});

						}
						console.log(res);

					},
					fail: () => {},

				});
			},
		}
	}
</script>
<style>
	.success-wrapper {
		display: flex;
		flex-direction: column;
		width: 100%;
		min-height: 100vh;
		background-color: #FFFFFF;
	}

	.pay-line {
		margin-top: 10%;
		width: 100%;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.pay-line image {
		width: 80upx;
		height: 80upx;
	}

	.pay-line text {
		color: #04BE02;
		font-size: 30upx;
		margin: 0 20upx;
		text-align: center;
	}

	.info-line {
		margin-top: 12%;
		width: 100%;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		color: #333333;
		font-size: 32upx;
	}



	.price-line {
		margin-top: 10upx;
		display: flex;
		flex-direction: row;
		color: #333333;
		font-size: 40upx;
		font-weight: bold;
	}

	.price-line .price {
		font-size: 60upx;

	}

	.bottom {
		position: absolute;
		bottom: 10%;
		display: flex;
		justify-content: center;
		align-items: center;
		width: 100%;
	}

	.bottom .btn {
		color: #747474;
		font-size: 28upx;
		width: 240upx;
		height: 60upx;
		line-height: 60upx;
		text-align: center;
		border: solid 2upx #04BE02;
		color: #04BE02;
		border-radius: 10upx;
	}
</style>
